package com.example.administrator.chinafood;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.chinafood.Adapter.MyListViewAdapter;
import com.example.administrator.chinafood.Adapter.TheForumListView;
import com.example.administrator.chinafood.SQLite.MyDBOpenHelper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class TheForum extends AppCompatActivity {

    private ListView list;
    private List<TheForumListView> mdata = null;
    private MyListViewAdapter<TheForumListView> myAdapter = null;

    private int personid;//根据该字段进行标识是哪个论坛，并且记录用户回复
    private MyDBOpenHelper mydbopenhelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.theforum);

        mydbopenhelper = new MyDBOpenHelper(TheForum.this,"ChinaFood.db");
        list = (ListView)findViewById(R.id.theforum_author_listView);
        setForum();
        init();

        Button btn_add = (Button)findViewById(R.id.theforum_author_btn);
        btn_add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addLiuyan();
            }
        });
    }

    private void addLiuyan(){
        SQLiteDatabase db = mydbopenhelper.getWritableDatabase();
        String user = "author3";
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm");
        String time = sdf.format(new Date().getTime());
        EditText ed_content = (EditText)findViewById(R.id.theforum_author_ed);
        String content = ed_content.getText().toString().trim();
        int img = R.drawable.img_1;
        db.execSQL("insert into liuyan (personid,user,time,content,img) values(?,?,?,?,?)"
                ,new Object[]{personid,user,time,content,img});
        mdata.add(new TheForumListView(img,user,time,content,personid));
        myAdapter.notifyDataSetChanged();

        //判断是否在此论坛第一次留言
        Cursor cursor = db.rawQuery("select * from liuyan where user = ? and personid = '"+personid+"'",new String[]{user});
        if (cursor.getCount() == 1){
            db.execSQL("insert into myliuyan (user,personid) values(?,?)",new Object[]{user,personid});
        }
        cursor.close();
        db.close();
    }

    private void setLiuyan(){
        SQLiteDatabase db = mydbopenhelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from liuyan where personid = '"+personid+"'",null);
        String user;
        String time;
        String content;
        int img;
        mdata.clear();
        while (cursor.moveToNext()){
            user = cursor.getString(cursor.getColumnIndex("user"));
            time = cursor.getString(cursor.getColumnIndex("time"));
            content = cursor.getString(cursor.getColumnIndex("content"));
            img = cursor.getInt(cursor.getColumnIndex("img"));
            mdata.add(new TheForumListView(img,user,time,content,personid));
        }
        cursor.close();
        db.close();
    }

    private void setForum(){
        TextView txt_title = (TextView)findViewById(R.id.theforum_author_title);
        TextView txt_author = (TextView)findViewById(R.id.theforum_author_name);
        TextView txt_time = (TextView)findViewById(R.id.theforum_author_time);
        TextView txt_content = (TextView)findViewById(R.id.theforum_author_content);
        TextView txt_num = (TextView)findViewById(R.id.theforum_author_num);
        ImageView iv_img = (ImageView)findViewById(R.id.theforum_author_img);

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String []string = bundle.getStringArray("string");
        int img = bundle.getInt("img");
        personid = bundle.getInt("personid");

        txt_title.setText(string[0]);
        txt_author.setText(string[1]);
        txt_time.setText(string[2]);
        txt_content.setText(string[3]);
        txt_num.setText(string[4]);
        iv_img.setImageResource(img);
    }

    private void init(){
        mdata = new ArrayList<TheForumListView>();
        setLiuyan();
        myAdapter = new MyListViewAdapter<TheForumListView>((ArrayList)mdata,R.layout.adapter_listview_theforum) {
            @Override
            public void bindView(ViewHolder holder, TheForumListView obj) {
                holder.setImageResource(R.id.theforum_listView_img,obj.getImg());
                holder.setText(R.id.theforum_listView_name,obj.getAuthor());
                holder.setText(R.id.theforum_listView_time,obj.getTime());
                holder.setText(R.id.theforum_listView_content,obj.getContent());
            }
        };
        list.setAdapter(myAdapter);
    }
}
